**The following code is taken verbatim from "Mous_ISQ DPUNR.do" except for the modifications noted by comments with AD. 

****Begin AD Modified Code****
clear

cd "$filetree"

clear 
use "MM_files/EUGMar11.dta", clear
****End AD Modified Code****

*ongoing years included, but not as Y events
drop if year <1947


merge 1:1 ccode1 ccode2 year using "MM_files/distance.dta"

*AD*merge 1:1 ccode1 ccode2 year using "C:\Users\mmousseau\Documents\Working Papers Data Sets\distance.dta"

drop if  _merge==2
drop  _merge

replace 	tpop_1		=. if tpop_1	 	==-9
replace 	tpop_2		=. if tpop_2	 	==-9
replace 	energy_1	=. if energy_1	==-9
replace 	energy_2	=. if energy_2	==-9
replace 	cap_1		=. if cap_1	 	==-9
replace 	cap_2		=. if cap_2	 	==-9
replace 	mzfatald	=. if mzfatald	==-9
replace 	polity21	=. if polity21	==-99
replace 	polity22	=. if polity22	==-99

g cpl = cap_1 if cap_1 < cap_2 & cap_1~=. & cap_2~=.
replace cpl = cap_2 if cap_2 <= cap_1 & cap_1~=. & cap_2~=.
g cph = cap_1 if cap_1 > cap_2 & cap_1~=. & cap_2~=.
replace cph = cap_2 if cap_2 >= cap_1 & cap_1~=. & cap_2~=.
g lncprt=ln(cph/cpl) if cph~=. & cpl~=.
drop cap_1 cap_2 cpl cph
label variable lncprt "Relative capability"
g mjpw=0
replace mjpw=1 if majpow1==1 | majpow2==1
drop majpow1 majpow2
label variable mjpw "Major power"


g cntg=0 
replace cntg = 1 if contig <= 5
drop contig
label variable cntg "Contiguity"


g dist=ln(distance) if distance~=.
drop distance
label variable dist "Distance"


g edv1= energy_1/ tpop_1
g edv2= energy_2/ tpop_2
g edvl= ln(edv1+1) if edv1 ~=. & edv2 ~=.
replace edvl= ln(edv2+1) if edv2<edv1 & edv1 ~=. & edv2 ~=.
drop energy_1 energy_2 
label variable edvl "WealthL"

sort ccode1 ccode2 year

g mzfmid=0 if mzmid~=. & mzfatald~=.
replace  mzfmid=1 if  mzfatald>=1 & mzmid==1 & mzfatald~=. & mzmid~=.

gen double ID= (1000000*ccode1)+ccode2
g mzfmidl = mzfmid[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1

**AD modified
label variable mzfmidl "MM Fatal MID Onset Lead" 
**END AD modified

g mzmidl = mzmid[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
**AD modified
label variable mzmidl "MM MID Onset Lead" 
**End AD modified


sort ID
btscs mzfmidl year ID, g(fpceyrs) nspline(3)
rename  _spline1  fspl1
rename  _spline2  fspl2
rename  _spline3  fspl3
btscs mzmidl year ID, g(apceyrs) nspline(3)
rename  _spline1  aspl1
rename  _spline2  aspl2
rename  _spline3  aspl3

rename mzmidl mzamidl

****Begin AD Modified Code****
label variable mzamidl "MM MID Onset Lead"

*Variable labels from EUGene
label variable mzongo "A MID was ongoing at beginning of year, Maoz dyadic data plus MID 3.0"
label variable mzongonm "Num of the MID that was ongoing at the beginning of the year, if any"
label variable mzmid "MID Onset: MID occurred betw cc1 and cc2 in year (0=no, 1=yes) (maoz data)."
label variable mzmidnm "Num of the new MID that began in this year, if any (0 if no MID was ongoing)"



*Constructing Fatal MID Ongoing and MID Ongoing in same erroneous way. 
g mzfmidong=0 if mzongo~=. & mzfatald~=.
replace  mzfmidong=1 if  mzfatald>=1 & mzongo==1 & mzfatald~=. & mzongo~=.
g mzfmidongl = mzfmidong[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
label variable mzfmidongl "MM Fatal MID Ongoing Lead"

sort ccode1 ccode2 year
g mzongol = mzongo[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
label variable mzongol "MM MID Ongoing Lead"

g dyadid=ccode1*10000+ccode2

g mzmido=1 if mzmid==1 | mzongo==1
replace mzmido=0 if mzmid==0 & mzongo==0
label variable mzmido "MID Ongoing"

*mzfmido means maoz fatal mid ongoing
g mzfmido=0 if mzfatald~=.
replace  mzfmido=1 if  mzfatald>=1  & mzfatald~=.
label variable mzfmido "Fatal MID Ongoing"

*Fatal MID Onset defined as a Fatal MID Ongoing, and last year there was not a Fatal MID with the same MID number. If there was, then Fatal MID Onset =.
g mzfmidon=mzfmido
sort dyadid year
by dyadid: replace mzfmidon=. if  mzfmido==1 & mzfmido[_n-1]==1 & dyadid==dyadid[_n-1] & mzkeynum==mzkeynum[_n-1]
label variable mzfmidon "Fatal MID Onset"
tab mzfmido mzfmidon, m 

g mzwaro=0 
replace mzwaro=1 if  mzfatald==6  

label variable mzwaro "War Ongoing"

sort ID year
g mzfmidol = mzfmido[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
g mzfmidonl = mzfmidon[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
g mzmidol=mzmido[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
g mzwarol=mzwaro[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1

label variable mzmidol "MID Ongoing Lead"
label variable mzfmidonl "Fatal MID Onset Lead"
label variable mzfmidol "Fatal MID Ongoing Lead"
label variable mzwarol "War Ongoing Lead"

*mzmidonl is corrected mid onset lead
g mzmidonl=mzmid[_n+1] if ID==ID[_n+1] & year[_n+1]==year+1
replace mzmidonl=. if mzmidonl==0 & mzongo[_n+1]==1
label variable mzmidonl "MID Onset Lead"

***Merging Original Beck and Webb data
rename year year1
sort ccode1 year1

merge m:1 ccode1 year1 using "beck_webb1.dta"
keep if _merge==3 | _merge==1
drop _merge


rename year1 year2
sort ccode2 year2

merge m:1 ccode2 year2 using "beck_webb2.dta"
keep if _merge==3 | _merge==1
drop _merge

rename year2 year
gen CIEnl=log(min(lifedeer1, lifedeer2)+1)  if lifedeer1~=. & lifedeer2~=.


****End AD Modified Code****



drop mzfmid upop_2 irst_2  milex_2 milper_2 upop_1 irst_1  milex_1 milper_1 mzongo mzongonm mzmid mzmidnm mzhost1 mzhost2 mzkeynum mzfatald mzpceyrs

*AD*g dyadid=ccode1*10000+ccode2

*AD*drop if year <1960

****Begin AD Modified Code****
***Avoid dropping all years***
gen MMsample=1 if year>=1960
*drop if year <1960

save "MM_precise.dta", replace

use "MM_precise.dta", clear
****End AD Modified Code****


*AD*save "C:\Users\mmousseau\Documents\Working Papers Data Sets\Cap Peace\DPUNRAV.dta", replace
*AD*use "C:\Users\mmousseau\Documents\Working Papers Data Sets\Cap Peace\DPUNRAV.dta", clear


*AD*
*AD*merge 1:1 year dyadid using "C:\Users\mmousseau\Documents\Working Papers Data Sets\Gleditsch Trade.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*

*AD*merge 1:1 dyadid year using "C:\Users\mmousseau\Documents\Working Papers Data Sets\Gartzke.dta"
*AD*drop  _merge
*AD*
*AD*sort ccode2 year
*AD*merge m:1 ccode2 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\PWT_Gleditsch2.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*

*AD*merge m:1 ccode2 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\McDon_2.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*

*AD*sort ccode1 year
*AD*merge m:1 ccode1 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\PWT_Gleditsch1.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*merge m:1 ccode1 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\McDon_1.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*

*AD*sort  ccode2 year
*AD*merge m:1 ccode2 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\CINE 2011.2.15 ccode2.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*sort  ccode1 year
*AD*merge m:1 ccode1 year using "C:\Users\mmousseau\Documents\Ifolder3\Ifolder\Working Papers\Data\CINE 2011.2.15 ccode1.dta"
*AD*drop if  _merge==2
*AD*drop  _merge
*AD*

*AD*

*AD*merge 1:1 ccode1 ccode2 year using "C:\Users\mmousseau\Documents\Working Papers Data Sets\Political Dissimilarity Werner.dta"
*AD*drop if  _merge==2
*AD*drop  _merge



****Begin AD Modified Code****

merge 1:1 year dyadid using "MM_files/Gleditsch Trade.dta"

drop if  _merge==2

drop  _merge



merge 1:1 dyadid year using "MM_files/Gartzke.dta"

drop  _merge



sort ccode2 year

merge m:1 ccode2 year using "MM_files/PWT_Gleditsch2.dta"

drop if  _merge==2

drop  _merge



merge m:1 ccode2 year using "MM_files/McDon_2.dta"

drop if  _merge==2

drop  _merge



sort ccode1 year

merge m:1 ccode1 year using "MM_files/PWT_Gleditsch1.dta"

drop if  _merge==2

drop  _merge

merge m:1 ccode1 year using "MM_files/McDon_1.dta"

drop if  _merge==2

drop  _merge



sort  ccode2 year

merge m:1 ccode2 year using "MM_files/CINE 2011.2.15 ccode2.dta"

**AD**
*This data file was apparently constructed using a version of "CINE Data Replication Do File.do" and "raw for replication.dta"
*****

drop if  _merge==2

drop  _merge

sort  ccode1 year

merge m:1 ccode1 year using "MM_files/CINE 2011.2.15 ccode1.dta"

drop if  _merge==2

drop  _merge





merge 1:1 ccode1 ccode2 year using "MM_files/Political Dissimilarity Werner.dta"

drop if  _merge==2

drop  _merge


save "MM_precise.dta", replace

use "MM_precise.dta", clear
****End AD Modified Code****

*AD*
*AD*save "C:\Users\mmousseau\Documents\Working Papers Data Sets\Cap Peace\DPUNRAV.dta", replace
*AD*use "C:\Users\mmousseau\Documents\Working Papers Data Sets\Cap Peace\DPUNRAV.dta", clear
*AD*


replace 	capopenl	=. if capopenl	==-9
replace 	pub1	=. if pub1		==-9
replace 	pub2	=. if pub2		==-9

g CIEl= CIE1 if CIE1~=. & CIE2~=.
replace CIEl= CIE2 if CIE1> CIE2 & CIE1~=. & CIE2~=.
label variable CIEl "CIEL"

g bCIE=0 if CIEd1~=. | CIEd2~=.
replace bCIE=1 if CIEd1==1 & CIEd2==1
label variable bCIE "CIEBinary"

g dml=polity21 if polity21~=. & polity22~=.
replace dml= polity22 if polity21> polity22 & polity21~=. & polity22~=.
g dmh=polity21 if polity21~=. & polity22~=.
replace dmh= polity22 if polity21< polity22 & polity21~=. & polity22~=.
g dmdist = dmh-dml if dmh~=. & dml~=.
*drop polity21 polity22 
label variable dml "DemocracyL"

label variable dmdist "Regime Difference"



****Begin AD Modified Code****
*recentering dml 75th percentile
sum dml, d
gen dmlc=dml-r(p75)

sum CIEl, d
gen CIElc=CIEl-r(p75)

sum CIEl, d
gen CIElc2=CIEl-r(p90)

gen dmlCIElc=dml*CIElc
gen dmlCIElc2=dml*CIElc2
****End AD Modified Code****


g bdm=0 if dml~=.
replace bdm=1 if dml >=7 & dml~=.
label variable bdm "DemocracyBinary6"


g h10dm=0 if  dml~=.
replace h10dm=1 if  dml==10 & dml~=.
replace h10dm=. if  dml==.
label variable h10dm "DemocracyBinary10"


gen dml2=dml+10

gen dmlsq=dml2^2

label variable dmlsq "DemocracyL^2"

g dvl= ln(rgdp96pc1) if rgdp96pc1 ~=. & rgdp96pc2 ~=.
replace dvl= ln(rgdp96pc2) if rgdp96pc2<rgdp96pc1 & rgdp96pc1 ~=. & rgdp96pc2 ~=.
drop rgdp96pc1 rgdp96pc2

g dpl=dp1*100 if dp1~=. & dp2~=.
replace dpl= dp2*100 if dp1> dp2 & dp1~=. & dp2~=.
drop dp1 dp2
label variable dpl "TradeL"

g pubh=pub1 if pub1~=. & pub2~=.
replace pubh= pub2 if pub2> pub1 & pub1~=. & pub2~=.
drop pub1 pub2
label variable pubh "PublicH"


sort dyadid
gen capopenl2=capopenl
replace capopenl2=0 if (capopenl==. & year>=1966 & year<1993)
labe variable capopenl  "CAPOPENL (Gartzke 2007)"
labe variable capopenl2 "CAPOPENL (Gartzke & Hewitt 2010)"
by dyadid: ipolate capopenl year, gen(capopenl_ipol)
gen capopenl_ipol2=capopenl_ipol
replace capopenl_ipol2=0 if (capopenl_ipol==. & year>=1966 & year<=1992)
labe variable capopenl_ipol2 "Capital OpennessL"

g misscpl=0
replace misscpl=1 if capopenl==. 
replace misscpl = . if year <1966
replace misscpl = . if year > 1992
tab misscpl mzfmidl

g incrrctmiss=0
replace incrrctmiss =1 if capopenl ==. & capopenl_ipol~=.
replace incrrctmiss = . if year <1966
replace incrrctmiss = . if year > 1992
tab incrrctmiss mzfmidl


sum year if year >1960



****Begin AD Modified Code****
replace MMsample=. if CIEl==.
*drop if CIEl==.
****End AD Modified Code****

*AD*drop if CIEl==.


****Begin AD Modified Code****
**"12-06-13_mids.dta" is a file that we constructed from scratch, in a manner similar to how Mousseau constructed his data file. 
*From: "AD_data_merge.do"
sort ccode1 ccode2 year
merge 1:1 ccode1 ccode2 year using "12-06-13_mids.dta"
drop if _merge==2
drop if _merge==1
drop _merge

drop midyears* fmidyears* waryears* 

sort ID
btscs mzmidol year ID, g(midyears) nspline(3)
rename  _spline1  midyears2
rename  _spline2  midyears3
rename  _spline3  midyears4


btscs mzfmidol year ID, g(fatalyears) nspline(3)
rename  _spline1  fatalyears2
rename  _spline2  fatalyears3
rename  _spline3  fatalyears4

save "MM_precise.dta", replace
****End AD Modified Code****

*AD*save "C:\Users\mmousseau\Documents\Working Papers Data Sets\Cap Peace\DPUNRAV.dta", replace




